1941F - Rudolf and Imbalance - CodeForces Solution


binary search greedy two pointers

Please click on ads to support us..

Python Code:

import heapq

t = int(input())
for _ in range(t):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))

    diffs = []
    for i in range(1, n):
        diffs.append((a[i]-a[i-1], a[i-1], a[i]))
    diffs.sort()

    if len(c) < len(b):
        b, c = c, b
    c = sorted(c)

    target = (diffs[-1][1] + diffs[-1][2]) / 2

    closest = diffs[-1][1]
    for x in b:
        l = 0
        r = len(c) - 1
        while l <= r:
            m = (l + r) // 2
            t = x + c[m]
            if abs(target - t) < abs(target - closest):
                closest = t
            if t < target:
                l = m + 1
            else:
                r = m - 1
    d1 = closest - diffs[-1][1]
    d2 = diffs[-1][2] - closest

    if len(diffs) == 1:
        print(max(d1, d2))
    else:
        print(max(diffs[-2][0], max(d1, d2)))


Comments

Submit
0 Comments
More Questions

1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas